#include <stdio.h>
#include <string.h>
#include "args.h"

static const char *base = NULL;
static struct seg_list arg_list;

void set_args(const char *buf, struct seg_list *args)
{
	base = buf;
	copy_seg(&arg_list, args);
}

void unset_args(void)
{
	seg_list_init(&arg_list);
}

static char argz[ARGS_LIMIT];

const char *pop_argz(void)
{
	seg_t seg = seg_list_pop(&arg_list);
	if (valid_seg(seg)) {
		seg_point_t start = start_seg(seg);
		strncpy(argz, base+start, len_of_seg(seg) + 1);
		argz[len_of_seg(seg)] = 0;
	} else 
		argz[0] = 0;
	return argz;
}

